package com.xwzn.resident.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xwzn.resident.BannerActivity;
import com.xwzn.resident.BaseToastView;
import com.xwzn.resident.BsznActivity;
import com.xwzn.resident.adapter.HomeShfwAdapter;
import com.xwzn.resident.LoginActivity;
import com.xwzn.resident.R;
import com.xwzn.resident.ShfwGdActivity;
import com.xwzn.resident.ShfwItemActivity;
import com.xwzn.resident.TzggActivity;
import com.xwzn.resident.adapter.HomeShfwXqAdapter;
import com.xwzn.resident.adapter.HomeTZRlVAdapter;
import com.xwzn.resident.loader.GlideImageLoader;
import com.xwzn.resident.model.HomeShfw;
import com.xwzn.resident.model.HomeShfwXq;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 首页界面
 * @Author HanKaiYang
 * @CreateDate 2019/12/23 11:00
 * @Version 1.0
 */
public class OneFragment extends Fragment implements View.OnClickListener {
    private View view;

    //轮播图
    private Banner banner;
    //通知公告
    private RecyclerView rlvTz;
    //生活服务
    private RecyclerView rlvShfw;
    //生活服务详情
    private RecyclerView rlvShfwXq;

    //轮播图片数组
    private int[] images;
    //轮播图片标题
    private String[] bannerTitles;
    //轮播图片时间
    private String[] bannerDates;
    //通知公告内容
    private List<String> tzList;
    //生活服务数据
    private List<HomeShfw> homeShfwList;
    //生活服务详情数据
    private List<HomeShfwXq> homeShfwXqList;
    private CardView cvHomeZyfw;
    private CardView cvHomeBszn;
    private TextView tvHomeGd;
    private ImageView ivHomeRzdj;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.home_fragment,container,false);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        init();
    }

    private void init() {

        initData();
        initView();
    }

    /**
     * @description 初始化数据
     * @author HanKaiYang
     * @createDate 2019/12/23 11:02
     * @version 1.0
     */
    private void initData() {
        //设置轮播图片
        images = new int[]{R.drawable.image_0, R.drawable.image_1, R.drawable.image_2};
        bannerTitles = new String[]{"机场立交", "轻轨世界", "今日龙湖"};
        bannerDates = new String[]{"2019-12-12 09:55:01", "2019-12-12 09:55:17", "2019-12-12 09:55:22"};
        tzList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            tzList.add(i + "通知奥术大师大啊是哒是哒asdasdasdsadasd");
        }

        //初始化生活服务的数据
        homeShfwList = new ArrayList<>();
        homeShfwList.add(new HomeShfw(R.mipmap.food, "美食"));
        homeShfwList.add(new HomeShfw(R.mipmap.shop, "购物"));
        homeShfwList.add(new HomeShfw(R.mipmap.bank, "银行"));
        homeShfwList.add(new HomeShfw(R.mipmap.scenic_spot, "景点"));
        homeShfwList.add(new HomeShfw(R.mipmap.accommodation, "住宿"));
        homeShfwList.add(new HomeShfw(R.mipmap.more_shfw, "更多"));

        homeShfwXqList = new ArrayList<>();
        homeShfwXqList.add(new HomeShfwXq(R.drawable.image_3, "KFC", "2019-12-12 08:57:17", "重庆市KFC总结"));

    }

    /**
     * @description 初始化视图
     * @author HanKaiYang
     * @createDate 2019/12/23 11:02
     * @version 1.0
     */
    private void initView() {
        banner = view.findViewById(R.id.banner);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setImageLoader(new GlideImageLoader());
        //轮播点击事件
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Log.d("轮播点击", position + "");
                Intent intent = new Intent(getActivity(), BannerActivity.class);
                intent.putExtra("image", images[position]);
                intent.putExtra("title", bannerTitles[position]);
                intent.putExtra("date", bannerDates[position]);
                startActivity(intent);
            }
        });

        //把轮播图数组拆分放入List数据里面
        List<Integer> imageList = new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            imageList.add(images[i]);
        }
        banner.setImages(imageList);
        //换图时间
        banner.setDelayTime(3000);
        banner.start();

        //通知公告
        rlvTz = view.findViewById(R.id.rlv_tz);
        HomeTZRlVAdapter homeTZRlVAdapter = new HomeTZRlVAdapter(tzList);
        rlvTz.setLayoutManager(new LinearLayoutManager(getContext()));
        rlvTz.setAdapter(homeTZRlVAdapter);
        homeTZRlVAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), TzggActivity.class);
                intent.putStringArrayListExtra("data", (ArrayList<String>) tzList);
                startActivity(intent);
            }
        });

        //入住登记
        ivHomeRzdj = view.findViewById(R.id.iv_home_rzdj);
        ivHomeRzdj.setOnClickListener(this);

        //生活服务
        rlvShfw = view.findViewById(R.id.rlv_shfw);
        final HomeShfwAdapter homeShfwAdapter = new HomeShfwAdapter(homeShfwList);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 6);
        rlvShfw.setLayoutManager(gridLayoutManager);
        rlvShfw.setAdapter(homeShfwAdapter);
        //生活服务点击事件
        homeShfwAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                BaseToastView toastView = new BaseToastView(getContext())
                        .initLayout(R.layout.home_dialog_layout,Gravity.CENTER);
                toastView.show();
            }
        });

        rlvShfwXq = view.findViewById(R.id.rlv_shfw_xq);
        final HomeShfwXqAdapter homeShfwXqAdapter = new HomeShfwXqAdapter(getContext(), homeShfwXqList);
        rlvShfwXq.setLayoutManager(new LinearLayoutManager(getContext()));
        rlvShfwXq.setAdapter(homeShfwXqAdapter);
        homeShfwXqAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), ShfwItemActivity.class);
                intent.putExtra("title",homeShfwXqAdapter.getData().get(position).getTitle());
                intent.putExtra("date",homeShfwXqAdapter.getData().get(position).getDate());
                intent.putExtra("content",homeShfwXqAdapter.getData().get(position).getContent());
                startActivity(intent);
            }
        });

        cvHomeZyfw = view.findViewById(R.id.cv_home_zyfw);
        cvHomeZyfw.setOnClickListener(this);
        cvHomeBszn = view.findViewById(R.id.cv_home_bszn);
        cvHomeBszn.setOnClickListener(this);
        tvHomeGd = view.findViewById(R.id.tv_home_gd);
        tvHomeGd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.iv_home_rzdj:
                intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.cv_home_zyfw:
                intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.cv_home_bszn:
                intent = new Intent(getActivity(), BsznActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_home_gd:
                intent = new Intent(getActivity(), ShfwGdActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
